class Solution
{
public:
    string reformatNumber(string number)
    {
        vector<char> pureNumbers;
        for (char c : number)
        {
            if (c != '-' && c != ' ')
            {
                pureNumbers.push_back(c);
            }
        }
        int n = pureNumbers.size();
        if (n <= 4)
        {
            return formRemainNums(n, pureNumbers.begin());
        }
        stringstream ss;

        for (int i = 0; i < n; ++i)
        {
            if ((n - i <= 4) && (i % 3 == 0))
            {
                ss << formRemainNums(n - i, pureNumbers.begin() + i);
                break;
            }
            else
            {
                ss << pureNumbers[i];
                if (i % 3 == 2)
                {
                    ss << '-';
                }
            }
        }
        return ss.str();
    }

    template <typename T>
    string formRemainNums(int remain, T it)
    {
        if (remain == 2 || remain == 3)
        {
            return string(it, it + remain);
        }
        if (remain == 4)
        {
            string result(it, it + 2);
            result += "-";
            result += string(it + 2, it + 4);
            return result;
        }
        return "";
    }
};